
import Pendataan.Pendataan;
import Pendataan.PendataanKontrol;
import Pendataan.pendataanTableModel;
import Pinjam.Data;
import Pinjam.PinjamKontrol;
//import Pinjam.pinjamTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import kembali.kembali;
import kembali.KembaliKontrol;
import kembali.kembaliTableModel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author user
 */
public class PinjamJInternalFrame1 extends javax.swing.JInternalFrame {

    /**
     * Creates new form PinjamJInternalFrame
     */
    public PinjamJInternalFrame1() {
        initComponents();
        NimTextFieldPinjam.setDocument(new JTextFieldFilter(JTextFieldFilter.numeric));
    }

    public class JTextFieldFilter extends PlainDocument {

        public static final String numeric = "0123456789";
        public static final String huruf = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXXYZ";
        protected String acceptedChars = null;
        protected boolean negativeAccepted = false;

        public JTextFieldFilter(String acceptedchars) {
            acceptedChars = acceptedchars;
        }

        public void setNegativeAccepted(boolean negativeaccepted) {
            if (acceptedChars.equals(numeric) || (acceptedChars.equals(huruf))) {
                negativeAccepted = negativeaccepted;
                acceptedChars += "_";
            }

        }

        public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
            if (str == null) {
                return;
            }

            for (int i = 0; i < str.length(); i++) {
                if (acceptedChars.indexOf(str.valueOf(str.charAt(i))) == -1) {
                    return;
                }
                if (negativeAccepted && str.indexOf("_") != -1) {
                    if (str.indexOf("_") != 0 || offset != 0) {
                        return;
                    }
                }
                super.insertString(offset, str, attr);
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jLabel3 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        judulBukuTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        NimTextFieldPinjam = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        penerbitTextField1 = new javax.swing.JTextField();
        pinjamPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        pinjamTable = new javax.swing.JTable();
        pinjamButton1 = new javax.swing.JButton();

        jLabel3.setText("jLabel3");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("Pinjam"), this, org.jdesktop.beansbinding.BeanProperty.create("title"));
        bindingGroup.addBinding(binding);

        jPanel2.setBackground(new java.awt.Color(204, 204, 255));

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(0, 0, 204));
        jLabel4.setText("PERPUSTAKAAN");

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(0, 0, 204));
        jLabel5.setText("UNIVERSITAS SANATA DHARMA");

        judulBukuTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                judulBukuTextField1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Judul Buku");

        NimTextFieldPinjam.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NimTextFieldPinjamActionPerformed(evt);
            }
        });

        jLabel2.setText("Nim");

        jLabel6.setText("penerbit");

        pinjamPanel.setBackground(new java.awt.Color(153, 153, 255));
        pinjamPanel.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                pinjamPanelAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });

        pinjamTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "judulBuku", "penerbit", "noBuku", "kuota"
            }
        ));
        pinjamTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                pinjamTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(pinjamTable);

        pinjamButton1.setText("Pinjam");
        pinjamButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pinjamButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pinjamPanelLayout = new javax.swing.GroupLayout(pinjamPanel);
        pinjamPanel.setLayout(pinjamPanelLayout);
        pinjamPanelLayout.setHorizontalGroup(
            pinjamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pinjamPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pinjamPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(pinjamButton1)
                .addGap(52, 52, 52))
        );
        pinjamPanelLayout.setVerticalGroup(
            pinjamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pinjamPanelLayout.createSequentialGroup()
                .addContainerGap(25, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(46, 46, 46)
                .addComponent(pinjamButton1)
                .addGap(81, 81, 81))
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(pinjamPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(32, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(107, 107, 107))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 315, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(penerbitTextField1)
                            .addComponent(NimTextFieldPinjam)
                            .addComponent(judulBukuTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE))
                        .addGap(47, 47, 47))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jLabel4)
                .addGap(18, 18, 18)
                .addComponent(jLabel5)
                .addGap(30, 30, 30)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(judulBukuTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(NimTextFieldPinjam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(31, 31, 31)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(penerbitTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(63, 63, 63)
                .addComponent(pinjamPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(87, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void pinjamButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pinjamButton1ActionPerformed

//        ResultSet rs = null;
        String judulBuku = judulBukuTextField1.getText();
        String nim = NimTextFieldPinjam.getText();

        Data data = new Data();
        data.setJudulBuku(judulBuku);
        data.setNIM(nim);

        kembali kem = new kembali();
        kem.setJudulBuku(judulBuku);
        kem.setNIM(nim);


        Pendataan kat = new Pendataan();
        String judul = judulBukuTextField1.getText();
        kat.setJudulBuku(judul);

        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        Calendar cal = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, 14);


        try {
            PinjamKontrol pinjam;
            pinjam = PinjamKontrol.getKoneksiPinjam();
            pinjam.PinjamData(data);


            PendataanKontrol Pend;
            Pend = PendataanKontrol.getKoneksiPendataan();
            Pend.kuotaPinjam(data);

            KembaliKontrol kembali;
            kembali = KembaliKontrol.getKoneksiKembali();
            kembali.KembaliData(kem);

//            pinjam.kuota(data);
//               pinjam.kuotaNOl(data);


            if (judulBukuTextField1.getText().equals("") || NimTextFieldPinjam.getText().equals("") || penerbitTextField1.getText().equals("")) {
                try {
                    throw new Exception("Ada field yang belum diisi");
                } catch (Exception z) {
                    JOptionPane.showMessageDialog(rootPane, "ERROR : " + z.getMessage());
                }
            } else {
                JOptionPane.showMessageDialog(this, "data berhasil disimpan \n"
                        + "NIM = " + nim + "\n"
                        + "judul buku = " + judulBuku + "\n"
                        + "tanggalPinjam =" + sdf.format(cal2.getTime()) + "\n"
                        + "tanggalKembali = " + sdf.format(cal.getTime()));
//                        + "tanggalPinjam = " + tglPinjam + "\n"
//                        + "tanggalKembali =" + tglKembali + "\n"


//            while (rs.next()) {

//                judulTextFieldPinjam.setText(judulBuku);
//                NimTextFieldPinjam.setText(nim);
//                tanggalPinjamTextFieldPinjam.setText(tglPinjam);
//                tanggalKembaliTextFieldPinjam2.setText(tglKembali);


            }
        } catch (SQLException ex) {

            JOptionPane.showMessageDialog(this, "Data Gagal Disimpan");
            Logger.getLogger(PinjamJInternalFrame1.class.getName()).log(Level.SEVERE, null, ex);
        }
//    


    }//GEN-LAST:event_pinjamButton1ActionPerformed

    private void judulBukuTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_judulBukuTextField1ActionPerformed
        // TODO add your handling code here:
        String judulBuku = judulBukuTextField1.getText();
        judulBukuTextField1.setText(judulBuku);
        try {
//            Barang cariBarang = BarangKontrol.getKoneksiBarang().cariberdasrkanKode(kodeBarang);
//            kembali kembaliBuku = kembaliKontrol1.getKoneksiKategori().cariberdasarkanJudul(judul);
            Pendataan pednataan = PendataanKontrol.getKoneksiPendataan().cariberdasrkanjudul(judulBuku);
            if (pednataan != null) {
//                penerbitTextField.setText(pednataan.getPenerbit());
//                isiStock();
                isiStock();

            } else {
                JOptionPane.showMessageDialog(null, "data tidak ditemukan");

            }
        } catch (SQLException ex) {
            Logger.getLogger(PinjamJInternalFrame1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_judulBukuTextField1ActionPerformed

    private void pinjamPanelAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_pinjamPanelAncestorAdded
//        cariButtonActionPerfor).addActionListener(new ActionListener() {
//        pinjamPanel.setVisible(false);
//action bila tombol ok di klik
        // TODO add your handling code here:
    }//GEN-LAST:event_pinjamPanelAncestorAdded

    private void NimTextFieldPinjamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NimTextFieldPinjamActionPerformed
        // TODO add your handling code here:
//        NimTextFieldPinjam.setVisible(false);
    }//GEN-LAST:event_NimTextFieldPinjamActionPerformed

    private void pinjamTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pinjamTableMouseClicked
        // TODO add your handling code here:
        int row = pinjamTable.getSelectedRow();
        String judulBuku = pinjamTable.getValueAt(row, 0).toString();
        String penerbit = pinjamTable.getValueAt(row, 1).toString();
        judulBukuTextField1.setText(judulBuku);
        penerbitTextField1.setText(penerbit);
        pinjamButton1.setEnabled(true);
//        simpanButton.setText("update");
    }//GEN-LAST:event_pinjamTableMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField NimTextFieldPinjam;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField judulBukuTextField1;
    private javax.swing.JTextField penerbitTextField1;
    private javax.swing.JButton pinjamButton1;
    private javax.swing.JPanel pinjamPanel;
    private javax.swing.JTable pinjamTable;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    public void isiStock() throws SQLException {
//        try {
        String judulBuku = judulBukuTextField1.getText();
// List<Pendataan> pendataan2 = PendataanKontrol.getKoneksiBarang().lihatseluruhKategori();
        List<Pendataan> pendataan = PendataanKontrol.getKoneksiPendataan().lihatseluruhKategori();

        pendataanTableModel model = new pendataanTableModel(pendataan);
        pinjamTable.setModel(model);
//        } catch (Exception ex) {
//            Logger.getLogger(PinjamJInternalFrame1.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }
};
